package com.leng.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;

public class DateUtil {
	/** 年月日时分秒(无下划线) yyyyMMddHHmmss */
	public static final String dtLong = "yyyyMMddHHmmss";

	/** 完整时间 yyyy-MM-dd HH:mm:ss */
	public static final String simple = "yyyy-MM-dd HH:mm:ss";

	/** 年月日(无下划线) yyyyMMdd */
	public static final String dtShort = "yyyy-MM-dd";

	/**
	 * 返回系统当前时间(精确到毫秒),作为一个唯一的订单编号
	 * 
	 * @return 以yyyyMMddHHmmss为格式的当前系统时间
	 */
	public static String getOrderNum() {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat(dtLong);
		return df.format(date);
	}

	/**
	 * 根据day获取日期开始时间，1为明天，-1为昨天，以此类推(0为今天)
	 * 
	 * @param day
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date getTodayStartTime(int day) {
		Calendar currentDate = new GregorianCalendar();
		currentDate.add(currentDate.DATE, day);// 把日期往后增加一天.整数往后推,负数往前移动
		currentDate.set(Calendar.HOUR_OF_DAY, 0);
		currentDate.set(Calendar.MINUTE, 0);
		currentDate.set(Calendar.SECOND, 0);
		return (Date) currentDate.getTime().clone();
	}

	/**
	 * 根据date获取日期开始时间，1为明天，-1为昨天，以此类推(0为今天)
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date getDateStartTime(Date date, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.add(Calendar.DATE, day);
		now.set(Calendar.HOUR_OF_DAY, 0);
		now.set(Calendar.MINUTE, 0);
		now.set(Calendar.SECOND, 0);
		return now.getTime();
	}

	/**
	 * 根据date获取日期结束时间，1为明天，-1为昨天，以此类推(0为今天)
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date getDateEndTime(Date date, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.add(Calendar.DATE, day);
		now.set(Calendar.HOUR_OF_DAY, 23);
		now.set(Calendar.MINUTE, 59);
		now.set(Calendar.SECOND, 59);
		return now.getTime();
	}

	/**
	 * 根据day获取日期结束时间，1为明天，-1为昨天，以此类推(0为今天)
	 * 
	 * @param day
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date getTodayEndTime(int day) {
		Calendar currentDate = new GregorianCalendar();
		currentDate.add(currentDate.DATE, day);// 把日期往后增加一天.整数往后推,负数往前移动
		currentDate.set(Calendar.HOUR_OF_DAY, 23);
		currentDate.set(Calendar.MINUTE, 59);
		currentDate.set(Calendar.SECOND, 59);
		return (Date) currentDate.getTime().clone();
	}

	/**
	 * 获取系统某个日期(精确到毫秒)，格式：yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String getOtherDateFormatter(Date date) {
		DateFormat df = new SimpleDateFormat(simple);
		return df.format(date);
	}

	/**
	 * 获取系统当前日期(精确到毫秒)，格式：yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String getDateFormatter() {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat(simple);
		return df.format(date);
	}

	/**
	 * 获取系统当期年月日(精确到天)，格式：yyyyMMdd
	 * 
	 * @return
	 */
	public static String getDate() {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat(dtShort);
		return df.format(date);
	}

	/**
	 * 返回指定格式的时间字符串 
	 * @param date
	 * @param pattern 如yyyy-MM-dd hh:mm:ss
	 * @return
	 */
	public static String DateToString(Date date,String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	/**
	 * 获取系统当期年月日(精确到天)，格式：yyyyMMdd
	 * 
	 * @return
	 */
	public static String getAfterDate(Date date) {

		DateFormat df = new SimpleDateFormat(dtShort);
		return df.format(date);
	}

	/**
	 * 产生随机的三位数
	 * 
	 * @return
	 */
	public static String getThree() {
		Random rad = new Random();
		return rad.nextInt(100000000) + "";
	}

	/**
	 * 得到几天前的时间
	 * 
	 * @param d
	 * @param day
	 * @return
	 */
	public static Date getDateBefore(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
		return now.getTime();
	}


	/**
	 * 将String格式日期转换为Date格式日期
	 * 
	 * @param date
	 *            类型
	 * @return
	 */
	public static Date getStringToDate(String d) {
		if (d == null || d == "") {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		try {
			date = sdf.parse(d);// 转回Date类型
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}


	/**
	 * /**
	 * <p>
	 * 描述：根据生日计算年龄
	 * <p>
	 * 
	 * @param birthday
	 * @return int
	 */
	public static Integer getAge(Date birthday) {
		if (null == birthday) {
			return null;
		}
		Calendar now = Calendar.getInstance();
		Calendar birth = Calendar.getInstance();
		birth.setTime(birthday);
		int age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
		birth.set(Calendar.YEAR, now.get(Calendar.YEAR));
		if (birth.after(now)) {
			age--;
		}
		return age;
	}

	/**
	 * 根据date获取日期开始时间，1为明天，-1为昨天，以此类推(0为今天)
	 * 
	 * @param day
	 * @return
	 */
	public static Date getDateStartTime(int day) {
		Calendar now = Calendar.getInstance();
		now.add(Calendar.DATE, day);
		now.set(Calendar.HOUR_OF_DAY, 0);
		now.set(Calendar.MINUTE, 0);
		now.set(Calendar.SECOND, 0);
		return now.getTime();
	}

	// 获取某个日期的开始时间
	public static Date getDateStart(Date date) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.HOUR_OF_DAY, 0);
		now.set(Calendar.MINUTE, 0);
		now.set(Calendar.SECOND, 0);
		return now.getTime();
	}

	// 计算两个日期相隔的天数
	public static long diff(Date start, Date end) {
		long startMills = start.getTime();
		long endMills = end.getTime();
		long diff = endMills - startMills;
		return diff / (3600000 * 24);
	}

	/**
	 * 根据date获取日期结束时间，1为明天，-1为昨天，以此类推(0为今天)
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date getDateEndTime(int day) {
		Calendar now = Calendar.getInstance();
		now.add(Calendar.DATE, day);
		now.set(Calendar.HOUR_OF_DAY, 23);
		now.set(Calendar.MINUTE, 59);
		now.set(Calendar.SECOND, 59);
		return now.getTime();
	}

	/*
	 * 得到几天后的时间
	 * 
	 * @param d
	 * 
	 * @param day
	 * 
	 * @return
	 */
	public static String getDateAfter(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return getAfterDate(now.getTime());
	}

	/**
	 * 得到传入时间所在周的周几
	 * 
	 * @param d
	 * @param weekDay
	 *            1周日 7周六
	 * @return
	 */
	public static Date getDayOfWeek(Date d, int weekDay) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DAY_OF_WEEK, weekDay);
		return now.getTime();
	}

	/**
	 * 得到几月前的时间
	 * 
	 * @param d
	 * @param month
	 * @return
	 */
	public static Date getDateBeforeMonth(Date d, int month) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.MONTH, now.get(Calendar.MONTH) - month);
		return now.getTime();
	}

	/**
	 * 得到几月后的时间
	 * 
	 * @param d
	 * @param month
	 * @return
	 */
	public static Date getDateAfterMonth(Date d, int month) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.MONTH, now.get(Calendar.MONTH) + month);
		return now.getTime();
	}

	public static Date getDateAfterMonth(int month) {
		Calendar now = Calendar.getInstance();
		now.set(Calendar.MONTH, now.get(Calendar.MONTH) + month);
		now.set(Calendar.HOUR_OF_DAY, 0);
		now.set(Calendar.MINUTE, 0);
		now.set(Calendar.SECOND, 0);
		return now.getTime();
	}

	/**
	 * 将Date格式日期转换为String格式日期
	 * 
	 * @param date
	 *            类型
	 * @return
	 */
	public static String getDateToString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String d = sdf.format(date); // 转换成字符串类型
		return d;
	}

	/**
	 * 将String格式日期转换为Date格式日期
	 * 
	 * @param date
	 *            类型
	 * @return
	 */
	public static Date getStringToDate(String d, String dateFormat) {
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		Date date = new Date();
		try {
			date = sdf.parse(d);// 转回Date类型
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 获取num分钟后的时间
	 * 
	 * @param date
	 *            当前时间
	 * @param num
	 *            数量
	 * @return
	 */
	public static Date getAfterNumMinutes(Date date, int num) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + num);
		return now.getTime();
	}

	public static Date getInstantFirst() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return c.getTime();
	}

	public static Date getInstantLast() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return c.getTime();
	}

	public static Date getLastMonthFirst() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return c.getTime();
	}

	public static Date getLastMonthLast() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return c.getTime();
	}

	public static Integer getYear(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.YEAR);
	}

	public static Integer getMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.MONTH) + 1;
	}

	public static Integer getDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(Calendar.DAY_OF_MONTH);
	}

	public static Date getFirstTimeByYearAndMonth(Integer year, Integer month) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return c.getTime();
	}

	public static Date getLastTimeByYearAndMonth(Integer year, Integer month) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return c.getTime();
	}

	public static Date getFormatDateToDate(Date date) throws ParseException {
		DateFormat df = new SimpleDateFormat(dtShort);
		date = df.parse(getDateToString(date));
		return date;
	}

	/**
	 * 
	 * getDateByYear 获取year年前/后的第一天
	 */
	public static Date getDateBeforeYear(Integer year) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, getYear(new Date()) - year);
		c.set(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return c.getTime();
	}

	/**
	 * 判断某个日期是不是今天
	 */
	public static boolean isToday(Date date) {
		if (null == date) {
			return false;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (sdf.format(date).equals(sdf.format(new Date()))) {
			return true;
		}
		return false;
	}

}
